package thread.synchroCode;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.LockSupport;

public class _11PrintFiveTimesParkUnpark {

  static Thread t1;
  static Thread t2;
  static Thread t3;

  public static void main(String[] args) {
    ParkUnpark pu = new ParkUnpark(6);
    t1 = new Thread(() -> {
      pu.print("a", t2);
    });
    t2 = new Thread(() -> {
      pu.print("b", t3);
    });
    t3 = new Thread(() -> {
      pu.print("c", t1);
    });

    t1.start();
    t2.start();
    t3.start();

    LockSupport.unpark(t1);
  }

  @Slf4j(topic = "131234123")
  static class ParkUnpark{
    private int loopNum;

    public void print(String str, Thread nextThread){
      for (int i = 0; i < loopNum; i++) {
        LockSupport.park();
        log.debug(str);
        LockSupport.unpark(nextThread);
      }
    }

    public ParkUnpark(int loopNum) {
      this.loopNum = loopNum;
    }
  }
}
